package com.xingda.packing.dmss.plc.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.xingda.packing.dmss.appset.AppSetService;
import com.xingda.packing.dmss.minitor.dto.AGVTaskDto;
import com.xingda.packing.dmss.minitor.entity.PkAgvTask;
import com.xingda.packing.dmss.minitor.entity.PkPlcLog;
import com.xingda.packing.dmss.minitor.mapper.PkPlcLogMapper;
import com.xingda.packing.dmss.plc.entity.PlcItem;
import com.xingda.packing.dmss.plc.entity.PlcItemVal;
import com.xingda.packing.dmss.plc.service.PlcDirectiveService;
import com.xingda.packing.dmss.plc.service.PlcHandleService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * PLC发送指令实现类
 *
 * @author LEGION
 */
@Slf4j
@Component
public class PlcDirectiveServiceImpl implements PlcDirectiveService {
    @Resource
    private PkPlcLogMapper pkPlcLogMapper;
    @Resource
    private PlcHandleService plcHandleService;

    @Resource
    private AppSetService appSetService;

    /**
     * 去起始位抓取
     *
     * @param locationNo 起始点
     * @param area       区域
     */
    @Override
    public void goZeroGrab(int locationNo, int area) {
        List<PlcItemVal> pivList = new ArrayList<>();
        pivList.add(new PlcItemVal("工字轮抓取位置号", getFieldQuery("GZLZQWZH", area), locationNo));
        pivList.add(new PlcItemVal("触发", getFieldQuery("CF", area), false));
        pivList.add(new PlcItemVal("任务位置", getFieldQuery("RWWZ", area), 1));
        pivList.add(new PlcItemVal("允许下发任务", getFieldQuery("YXXFRW", area), false));
        pivList.add(new PlcItemVal("机器人抓取完成", getFieldQuery("JQRZQWC", area), false));
        pivList.add(new PlcItemVal("机器人码垛完成", getFieldQuery("JQRMDWC", area), false));
        pivList.add(new PlcItemVal("MES下发任务完成", getFieldQuery("MESXFRWWC", area), true));
        log.info("前往传送带抓取位抓取任务下发成功-抓取位：{},区域{}", locationNo, area);
        if (!plcHandleService.writeItem(pivList)) {
            log.error("前往传送带抓取位抓取任务下发失败-抓取位：{},区域{}", locationNo, area);
        }
        pkPlcLogMapper.insert(new PkPlcLog(area, pivList.toString(), new Date()));
    }

    /**
     * 抓取起始位放置缓存位
     *
     * @param wheelTypeCode 工字轮类型
     * @param cacheLocation 缓存位编号
     * @param area          区域
     */
    @Override
    public void startStorage(int wheelTypeCode, int cacheLocation, int area) {
        // 放缓存位置号
        List<PlcItemVal> pivList = new ArrayList<>();
        pivList.add(new PlcItemVal("缓存台码垛位置号", getFieldQuery("HCTMDWZH", area), cacheLocation));
        pivList.add(new PlcItemVal("产品类别", getFieldQuery("CPLB", area), wheelTypeCode));
        pivList.add(new PlcItemVal("任务位置", getFieldQuery("RWWZ", area), 3));
        pivList.add(new PlcItemVal("允许下发任务", getFieldQuery("YXXFRW", area), false));
        pivList.add(new PlcItemVal("机器人抓取完成", getFieldQuery("JQRZQWC", area), false));
        pivList.add(new PlcItemVal("机器人码垛完成", getFieldQuery("JQRMDWC", area), false));
        pivList.add(new PlcItemVal("MES下发任务完成", getFieldQuery("MESXFRWWC", area), true));
        log.info("抓取位存放缓存位任务下发成功,工字轮类型:{},缓存位编号:{},区域:{}", wheelTypeCode, cacheLocation, area);
        if (!plcHandleService.writeItem(pivList)) {
            log.error("存放缓存位任务下发失败-缓存位：{}", cacheLocation);
        }
        pkPlcLogMapper.insert(new PkPlcLog(area, pivList.toString(), new Date()));
    }

    /**
     * 抓取起始位放装箱位
     *
     * @param wheelTypeCode 工字轮类型
     * @param boxLocationNo 装箱位编号
     * @param area          区域
     */
    @Override
    public void startGrab(int wheelTypeCode, int boxLocationNo, int area) {
        // 给机器人发指令 托盘号
        List<PlcItemVal> pivList = new ArrayList<>();
        pivList.add(new PlcItemVal("托盘号", getFieldQuery("TPH", area), boxLocationNo));
        pivList.add(new PlcItemVal("产品类别", getFieldQuery("CPLB", area), wheelTypeCode));
        pivList.add(new PlcItemVal("任务位置", getFieldQuery("RWWZ", area), 2));
        pivList.add(new PlcItemVal("允许下发任务", getFieldQuery("YXXFRW", area), false));
        pivList.add(new PlcItemVal("机器人抓取完成", getFieldQuery("JQRZQWC", area), false));
        pivList.add(new PlcItemVal("机器人码垛完成", getFieldQuery("JQRMDWC", area), false));
        pivList.add(new PlcItemVal("MES下发任务完成", getFieldQuery("MESXFRWWC", area), true));
        log.info("抓取位存放装箱位任务下发成功,码垛托盘号:{},工字轮类型:{},区域:{}", boxLocationNo, wheelTypeCode, area);
        if (!plcHandleService.writeItem(pivList)) {
            log.error("存放装箱位任务下发失败-码垛托盘号：{}", boxLocationNo);
        }
        pkPlcLogMapper.insert(new PkPlcLog(area, pivList.toString(), new Date()));
    }

    /**
     * 抓取缓存位放装箱位
     *
     * @param cacheLocationNo 缓存位置编号
     * @param boxLocationNo   装箱位编号
     * @param productType     产品类型
     * @param wheelNo         条形码
     * @param area            区域
     */
    @Override
    public void startCacheGrab(int cacheLocationNo, int boxLocationNo, int productType, String wheelNo, int area) {
        if (productType == -1 || productType == 0) {
            log.error("产品类型出错：{}", productType);
            return;
        }
        List<PlcItemVal> pivList = new ArrayList<>();
        pivList.add(new PlcItemVal("工字轮抓取位置号", getFieldQuery("GZLZQWZH", area), cacheLocationNo));
        pivList.add(new PlcItemVal("托盘号", getFieldQuery("TPH", area), boxLocationNo));
        pivList.add(new PlcItemVal("产品类别", getFieldQuery("CPLB", area), productType));
        pivList.add(new PlcItemVal("任务位置", getFieldQuery("RWWZ", area), 4));
        pivList.add(new PlcItemVal("允许下发任务", getFieldQuery("YXXFRW", area), false));
        pivList.add(new PlcItemVal("机器人抓取完成", getFieldQuery("JQRZQWC", area), false));
        pivList.add(new PlcItemVal("机器人码垛完成", getFieldQuery("JQRMDWC", area), false));
        char[] wheelNos = wheelNo.toCharArray();
        if (wheelNos.length <= 20) {
            int length = wheelNos.length;
            for (int i = 0; i < length; i++) {
                int no = wheelNos[i];
                int num = i + 1;
                String key = "code" + num;
                PlcItemVal piv = new PlcItemVal(key, getFieldQuery(key, area), no);
                pivList.add(piv);
            }
        }
        pivList.add(new PlcItemVal("MES下发任务完成", getFieldQuery("MESXFRWWC", area), true));
        boolean result = plcHandleService.writeItem(pivList);
        if (result) {
            log.info("抓取缓存位放装箱位成功,缓存位:{},装箱位:{},产品类型:{},条形码:{},区域:{}", cacheLocationNo, boxLocationNo, productType, wheelNo, area);
        }
        if (!result) {
            log.error("存放缓存位任务下发失败-缓存位：{}，装箱位{}", cacheLocationNo, boxLocationNo);
        }
        pkPlcLogMapper.insert(new PkPlcLog(area, pivList.toString(), new Date()));
    }

    /**
     * 轮号接受完成
     *
     * @param productType 产品类别
     * @param area        区域
     */
    @Override
    public void receive(int productType, int area) {
        List<PlcItemVal> pivList = new ArrayList<>();
        pivList.add(new PlcItemVal("产品类别", getFieldQuery("CPLB", area), productType));
        pivList.add(new PlcItemVal("触发", getFieldQuery("CF", area), false));
        pivList.add(new PlcItemVal("MES解析完成", getFieldQuery("MESJXWC", area), true));
        plcHandleService.writeItem(pivList);
        log.info("轮号接收完成,产品类别{},区域{}", productType, area);
        pkPlcLogMapper.insert(new PkPlcLog(area, pivList.toString(), new Date()));
    }

    /**
     * 条形码读取触发
     *
     * @param area 区域
     * @return 触发完成
     */
    @Override
    public boolean isScanSignal(int area) {
        JSONObject json = plcHandleService.readItem(new PlcItem("触发", getFieldQuery("CF", area)));
        // 正在循环、触发为true ，轮号接受完成为false
        boolean result = json.getBooleanValue("触发");
        if (result) {
            log.info("符合读取条形码条件...,结果{}", true);
            return true;
        }
        return false;
    }

    /**
     * 查询码垛完成情况
     *
     * @param area 区域
     */
    @Override
    public boolean isPalletizingComplete(int area) {
        JSONObject json = plcHandleService.readItem(new PlcItem("机器人码垛完成", getFieldQuery("JQRMDWC", area)));
        return json.getBooleanValue("机器人码垛完成");
    }

    /**
     * 重置码垛完成
     *
     * @param area 区域
     */
    @Override
    public void resetPalletizingState(int area) {
        List<PlcItemVal> pivList = new ArrayList<>();
        pivList.add(new PlcItemVal("机器人码垛完成", getFieldQuery("JQRMDWC", area), false));
        plcHandleService.writeItem(pivList);
        pkPlcLogMapper.insert(new PkPlcLog(area, pivList.toString(), new Date()));
    }

    /**
     * 重置抓取完成
     *
     * @param area 区域
     */
    @Override
    public void resetGrabState(int area) {
        List<PlcItemVal> pivList = new ArrayList<>();
        pivList.add(new PlcItemVal("机器人抓取完成", getFieldQuery("JQRZQWC", area), false));
        plcHandleService.writeItem(pivList);
        pkPlcLogMapper.insert(new PkPlcLog(area, pivList.toString(), new Date()));
    }

    /**
     * 查询抓取完成
     *
     * @param area 区域
     */
    @Override
    public boolean isGrabComplete(int area) {
        JSONObject json = plcHandleService.readItem(new PlcItem("机器人抓取完成", getFieldQuery("JQRZQWC", area)));
        return json.getBooleanValue("机器人抓取完成");
    }

    /**
     * 查询是否运行下发任务
     *
     * @param area 区域
     */
    @Override
    public boolean canAssignTask(int area) {
        JSONObject json = plcHandleService.readItem(new PlcItem("允许下发任务", getFieldQuery("YXXFRW", area)));
        return json.getBooleanValue("允许下发任务");
    }

    /**
     * 抓取位读码
     *
     * @param area 区域
     */
    @Override
    public String getScanWheelNo(int area) {
        JSONObject json = plcHandleService.readItem(new PlcItem("条码", getFieldQuery("TXM", area)));
        return json.getString("条码");
    }

    @Override
    public boolean clearCacheLocationNo(int locationNo, int area) {
        List<PlcItemVal> pivList = new ArrayList<>();
        pivList.add(new PlcItemVal("缓存位位置号", getFieldQuery("SCHCH", area), locationNo));
        pivList.add(new PlcItemVal("删除缓存", getFieldQuery("SCHC", area), true));
        boolean result = plcHandleService.writeItem(pivList);
        if (result) {
            log.info("删除缓存位删除成功,缓存位置号:{},区域:{}", locationNo, area);
            pkPlcLogMapper.insert(new PkPlcLog(area, pivList.toString(), new Date()));
            return true;
        }
        log.error("删除缓存位删除失败,缓存位置号:{},区域:{}", locationNo, area);
        return false;
    }

    @Override
    public void resetCacheStatus(int area) {
        List<PlcItemVal> pivList = new ArrayList<>();
        pivList.add(new PlcItemVal("删除缓存", getFieldQuery("SCHC", area), false));
        plcHandleService.writeItem(pivList);
        pkPlcLogMapper.insert(new PkPlcLog(area, pivList.toString(), new Date()));
    }

    private String getFieldQuery(String key) {
        JSONObject json = appSetService.getPlcNodes().getJSONObject(key);
        if (json == null) {
            log.error("配置文件未找到该PLC点位:{}", key);
            return null;
        }
        return json.getString("address");
    }

    private String getFieldQuery(String key, int area) {
        JSONObject json = appSetService.getPlcNodes().getJSONObject(key);
        if (json == null) {
            log.error("配置文件未找到该PLC点位:{}", key);
            return null;
        }
        return area == 1 ? json.getString("address1") : json.getString("address2");
    }

    @Override
    public void writeVals(PlcItemVal... plcItemVals) {
        boolean result = plcHandleService.writeItem(plcItemVals);
        if (!result) {
            log.error("初始设置装箱位失败！");
        }
    }

    @Override
    public boolean isTrue(String address) {
        JSONObject json = plcHandleService.readItem(new PlcItem("isTrue", address));
        return json.getBooleanValue("isTrue");
    }

    @Override
    public JSONObject readVals(PlcItem... plcItems) {
        return plcHandleService.readItem(plcItems);
    }

    @Override
    public JSONObject readVals(List<PlcItem> plcItems) {
        PlcItem[] pi = new PlcItem[plcItems.size()];
        plcItems.toArray(pi);
        return readVals(pi);
    }

    @Override
    public boolean isPhotoFinish(String address) {
        JSONObject json = plcHandleService.readItem(new PlcItem("拍照完成", address));
        return json.getBooleanValue("拍照完成");
    }

    @Override
    public boolean isHaveWheel(int area) {
        JSONObject json = plcHandleService.readItem(new PlcItem("抓取位有轮", getFieldQuery("ZQWSFYL", area)));
        return json.getBooleanValue("抓取位有轮");
    }

    @Override
    public boolean getRobotStatus(int area) {
        JSONObject json = plcHandleService.readItem(new PlcItem("强制停止机器人", getFieldQuery("QZTZJQR", area)));
        return json.getBooleanValue("强制停止机器人");
    }

    @Override
    public boolean isShuntAreaSignal() {
        JSONObject json = plcHandleService.readItem(new PlcItem("触发MES解析条码", getFieldQuery("FLQCFTMJX")));
        return json.getBooleanValue("触发MES解析条码");
    }

    @Override
    public boolean isSnapSignal() {
        JSONObject json = plcHandleService.readItem(new PlcItem("触发MES解析卡扣", getFieldQuery("FLQCFKKJX")));
        return json.getBooleanValue("触发MES解析卡扣");
    }

    @Override
    public String getShuntAreaScanWheelNo() {
        JSONObject json = plcHandleService.readItem(new PlcItem("分流区条码", getFieldQuery("FLQTXM")));
        return json.getString("分流区条码");
    }


    @Override
    public void writeValueToShuntArea(int area, String code) {
        List<PlcItemVal> pivList = new ArrayList<>();
        pivList.add(new PlcItemVal("分流区域", getFieldQuery("FLQY"), area));
        char[] wheelNos = code.toCharArray();
        if (wheelNos.length <= 20) {
            int length = wheelNos.length;
            for (int i = 0; i < length; i++) {
                int no = wheelNos[i];
                int num = i + 1;
                String key = "MESJXCTM" + num;
                PlcItemVal piv = new PlcItemVal(key, getFieldQuery(key), no);
                pivList.add(piv);
            }
        }
        boolean result = plcHandleService.writeItem(pivList);
        pkPlcLogMapper.insert(new PkPlcLog(3, pivList.toString(), new Date()));
        if (!result) {
            log.error("分流区域写入失败!区域:{},条码:{}", area, code);
        }
    }

    @Override
    public boolean isShuntOver() {
        JSONObject json = plcHandleService.readItem(new PlcItem("分流完成", getFieldQuery("FLQFLWC")));
        return json.getBooleanValue("分流完成");
    }

    @Override
    public void restartShuntAreaSignal() {
        List<PlcItemVal> pivList = new ArrayList<>();
        pivList.add(new PlcItemVal("分流完成", getFieldQuery("FLQFLWC"), false));
        boolean result = plcHandleService.writeItem(pivList);
        pkPlcLogMapper.insert(new PkPlcLog(3, pivList.toString(), new Date()));
        if (!result) {
            log.error("分流区重置失败");
        }
    }

    @Override
    public int getProductTypeToShunt() {
        JSONObject jsonObject = plcHandleService.readItem(new PlcItem("高低轮", getFieldQuery("FLQGDL")));
        return jsonObject.getIntValue("高低轮");
    }

    @Override
    public int getProductTypeToZeroGrab(int area) {
        JSONObject jsonObject = plcHandleService.readItem(new PlcItem("高低轮", getFieldQuery("GDL", area)));
        return jsonObject.getIntValue("高低轮");
    }

    @Override
    public int getProductType(int area) {
        JSONObject jsonObject = plcHandleService.readItem(new PlcItem("产品类别", getFieldQuery("CPLB", area)));
        return jsonObject.getIntValue("产品类别");
    }

    @Override
    public int getPalletPackingCount(String address) {
        return plcHandleService.readItem(new PlcItem("count", address)).getIntValue("count");
    }

    @Override
    public int getPalletWheelNumSet(String address) {
        JSONObject jsonObject = plcHandleService.readItem(new PlcItem("num", address));
        return jsonObject.getIntValue("num");
    }

    @Override
    public boolean getMesTask(int area) {
        JSONObject jsonObject = plcHandleService.readItem(new PlcItem("MES下发任务完成", getFieldQuery("MESXFRWWC", area)));
        return jsonObject.getBooleanValue("MES下发任务完成");
    }

    @Override
    public int getTaskType(int area) {
        JSONObject jsonObject = plcHandleService.readItem(new PlcItem("任务位置", getFieldQuery("RWWZ", area)));
        return jsonObject.getIntValue("任务位置");
    }

    @Override
    public void grabWheel(int area) {
        List<PlcItemVal> pivList = new ArrayList<>();
        pivList.add(new PlcItemVal(new PlcItem("任务位置", getFieldQuery("RWWZ", area)), 5));
        plcHandleService.writeItem(pivList);
        pkPlcLogMapper.insert(new PkPlcLog(area, pivList.toString(), new Date()));
    }

    @Override
    public boolean getPlcStatus() {
        JSONObject jsonObject = plcHandleService.readItem(new PlcItem("MES心跳", getFieldQuery("MESXT")));
        return jsonObject.getBooleanValue("MES心跳");
    }

    @Override
    public void writePlcStatus() {
        List<PlcItemVal> pivList = new ArrayList<>();
        pivList.add(new PlcItemVal(new PlcItem("MES心跳", getFieldQuery("MESXT")), false));
        plcHandleService.writeItem(pivList);
        pkPlcLogMapper.insert(new PkPlcLog(3, pivList.toString(), new Date()));
    }

    @Override
    public void codeParsingSucceeded() {
        List<PlcItemVal> pivList = new ArrayList<>();
        pivList.add(new PlcItemVal(new PlcItem("触发MES解析条码", getFieldQuery("FLQCFTMJX")), false));
        pivList.add(new PlcItemVal(new PlcItem("MES解析条码完成", getFieldQuery("FLQJXTMWC")), true));
        plcHandleService.writeItem(pivList);
        pkPlcLogMapper.insert(new PkPlcLog(3, pivList.toString(), new Date()));
    }

    @Override
    public void codeParsingFailed() {
        List<PlcItemVal> pivList = new ArrayList<>();
        pivList.add(new PlcItemVal(new PlcItem("触发MES解析条码", getFieldQuery("FLQCFTMJX")), false));
        pivList.add(new PlcItemVal(new PlcItem("MES解析条码错误", getFieldQuery("FLQJXTMCW")), true));
        plcHandleService.writeItem(pivList);
        pkPlcLogMapper.insert(new PkPlcLog(3, pivList.toString(), new Date()));
    }

    @Override
    public void snapParsingSucceeded() {
        List<PlcItemVal> pivList = new ArrayList<>();
        pivList.add(new PlcItemVal(new PlcItem("触发MES解析卡扣", getFieldQuery("FLQCFKKJX")), false));
        pivList.add(new PlcItemVal(new PlcItem("MES解析卡扣完成", getFieldQuery("FLQKKJXWC")), true));
        plcHandleService.writeItem(pivList);
        pkPlcLogMapper.insert(new PkPlcLog(3, pivList.toString(), new Date()));
    }

    @Override
    public void snapParsingFailed() {
        List<PlcItemVal> pivList = new ArrayList<>();
        pivList.add(new PlcItemVal(new PlcItem("触发MES解析卡扣", getFieldQuery("FLQCFKKJX")), false));
        pivList.add(new PlcItemVal(new PlcItem("MES解析卡扣错误", getFieldQuery("FLQKKJXCW")), true));
        plcHandleService.writeItem(pivList);
        pkPlcLogMapper.insert(new PkPlcLog(3, pivList.toString(), new Date()));
    }

    @Override
    public boolean isCodeParsingSucceeded() {
        JSONObject jsonObject = plcHandleService.readItem(new PlcItem("MES解析条码完成", getFieldQuery("FLQJXTMWC")));
        return jsonObject.getBooleanValue("MES解析条码完成");
    }

    @Override
    public boolean isCodeParsingFailed() {
        JSONObject jsonObject = plcHandleService.readItem(new PlcItem("MES解析条码错误", getFieldQuery("FLQJXTMCW")));
        return jsonObject.getBooleanValue("MES解析条码错误");
    }

    @Override
    public boolean isSnapParsingSucceeded() {
        JSONObject jsonObject = plcHandleService.readItem(new PlcItem("MES解析卡扣完成", getFieldQuery("FLQKKJXWC")));
        return jsonObject.getBooleanValue("MES解析卡扣完成");
    }

    @Override
    public boolean isSnapParsingFailed() {
        JSONObject jsonObject = plcHandleService.readItem(new PlcItem("MES解析卡扣错误", getFieldQuery("FLQKKJXCW")));
        return jsonObject.getBooleanValue("MES解析卡扣错误");
    }

    @Override
    public boolean isStopSignal() {
        JSONObject jsonObject = plcHandleService.readItem(new PlcItem("内收线紧急停止", getFieldQuery("NSXTZ")));
        return !jsonObject.getBooleanValue("内收线紧急停止");
    }

    @Override
    public boolean isReset() {
        JSONObject jsonObject = plcHandleService.readItem(new PlcItem("复位AGV", getFieldQuery("FWAGV")));
        return jsonObject.getBooleanValue("复位AGV");
    }

    @Override
    public boolean isPacking() {
        JSONObject jsonObject = plcHandleService.readItem(new PlcItem("AGV在取料区域", getFieldQuery("AGVQLQY")));
        return jsonObject.getBooleanValue("AGV在取料区域");
    }

    @Override
    public boolean isPackingOk() {
        JSONObject jsonObject = plcHandleService.readItem(new PlcItem("AGV取料完成", getFieldQuery("AGVQLWC")));
        return jsonObject.getBooleanValue("AGV取料完成");
    }

    @Override
    public boolean isPackingStatus() {
        JSONObject jsonObject = plcHandleService.readItem(new PlcItem("AGV请求取料", getFieldQuery("AGVQL")));
        return jsonObject.getBooleanValue("AGV请求取料");
    }

    @Override
    public boolean isKongTuo() {
        JSONObject jsonObject = plcHandleService.readItem(new PlcItem("AGV在放托盘区域", getFieldQuery("AGVTPQY")));
        return jsonObject.getBooleanValue("AGV在放托盘区域");
    }

    @Override
    public boolean isKongTuoOk() {
        JSONObject jsonObject = plcHandleService.readItem(new PlcItem("AGV放托盘完成", getFieldQuery("AGVTPWC")));
        return jsonObject.getBooleanValue("AGV放托盘完成");
    }

    @Override
    public boolean isKongTuoStatus() {
        JSONObject jsonObject = plcHandleService.readItem(new PlcItem("AGV请求放托盘", getFieldQuery("AGVFTP")));
        return jsonObject.getBooleanValue("AGV请求放托盘");
    }

    public boolean allowTask1() {
        JSONObject jsonObject = plcHandleService.readItem(new PlcItem("内收线允许取料", getFieldQuery("NSXYXQL")));
        return jsonObject.getBooleanValue("内收线允许取料");
    }

    public boolean allowTask2() {
        JSONObject jsonObject = plcHandleService.readItem(new PlcItem("内收线允许放托盘", getFieldQuery("NSXYXTP")));
        return jsonObject.getBooleanValue("内收线允许放托盘");
    }

    @Override
    public void saveToPlc(PkAgvTask pkAgvTask) {
        List<PlcItemVal> pivList = new ArrayList<>();
        if (pkAgvTask.getType() == 1) {
            pivList.add(new PlcItemVal(new PlcItem("AGV满轮托盘号", getFieldQuery("AGVMLTPH")), pkAgvTask.getPosition()));
            pivList.add(new PlcItemVal(new PlcItem("AGV请求取料", getFieldQuery("AGVQL")), true));
            pivList.add(new PlcItemVal(new PlcItem("AGV在取料区域", getFieldQuery("AGVQLQY")), false));
        } else {
            pivList.add(new PlcItemVal(new PlcItem("AGV请求放托盘号", getFieldQuery("AGVFTPH")), pkAgvTask.getPosition()));
            pivList.add(new PlcItemVal(new PlcItem("AGV请求放托盘", getFieldQuery("AGVFTP")), true));
            pivList.add(new PlcItemVal(new PlcItem("AGV在放托盘区域", getFieldQuery("AGVTPQY")), false));
        }
        plcHandleService.writeItem(pivList);
        pkPlcLogMapper.insert(new PkPlcLog(4, pivList.toString(), new Date()));
    }

    @Override
    public void agvRunning(AGVTaskDto agvTaskDto) {
        List<PlcItemVal> pivList = new ArrayList<>();
        if (agvTaskDto.getType() == 1) {
            pivList.add(new PlcItemVal(new PlcItem("AGV在取料区域", getFieldQuery("AGVQLQY")), true));
        } else {
            pivList.add(new PlcItemVal(new PlcItem("AGV在放托盘区域", getFieldQuery("AGVTPQY")), true));
        }
        plcHandleService.writeItem(pivList);
        pkPlcLogMapper.insert(new PkPlcLog(4, pivList.toString(), new Date()));
    }

    @Override
    public void agvComplete(AGVTaskDto agvTaskDto) {
        List<PlcItemVal> pivList = new ArrayList<>();
        if (agvTaskDto.getType() == 1) {
            pivList.add(new PlcItemVal(new PlcItem("AGV取料完成", getFieldQuery("AGVQLWC")), true));
            pivList.add(new PlcItemVal(new PlcItem("AGV请求取料", getFieldQuery("AGVQL")), false));
            pivList.add(new PlcItemVal(new PlcItem("AGV在取料区域", getFieldQuery("AGVQLQY")), false));
            pivList.add(new PlcItemVal(new PlcItem("AGV满轮托盘号", getFieldQuery("AGVMLTPH")), 0));
        } else {
            pivList.add(new PlcItemVal(new PlcItem("AGV放托盘完成", getFieldQuery("AGVTPWC")), true));
            pivList.add(new PlcItemVal(new PlcItem("AGV请求放托盘", getFieldQuery("AGVFTP")), false));
            pivList.add(new PlcItemVal(new PlcItem("AGV在放托盘区域", getFieldQuery("AGVTPQY")), false));
            pivList.add(new PlcItemVal(new PlcItem("AGV请求放托盘号", getFieldQuery("AGVFTPH")), 0));
        }
        plcHandleService.writeItem(pivList);
        pkPlcLogMapper.insert(new PkPlcLog(4, pivList.toString(), new Date()));
    }

    @Override
    public int agvTPH1() {
        JSONObject jsonObject = plcHandleService.readItem(new PlcItem("AGV满轮托盘号", getFieldQuery("AGVMLTPH")));
        return jsonObject.getIntValue("AGV满轮托盘号");
    }

    @Override
    public int agvTPH2() {
        JSONObject jsonObject = plcHandleService.readItem(new PlcItem("AGV请求放托盘号", getFieldQuery("AGVFTPH")));
        return jsonObject.getIntValue("AGV请求放托盘号");
    }

    @Override
    public void agvInsert1(String s1, String s2, int value) {
        List<PlcItemVal> pivList = new ArrayList<>();
        pivList.add(new PlcItemVal(new PlcItem(s1, getFieldQuery(s2)), value));
        plcHandleService.writeItem(pivList);
    }

    @Override
    public void agvInsert2(String s1, String s2, boolean value) {
        List<PlcItemVal> pivList = new ArrayList<>();
        pivList.add(new PlcItemVal(new PlcItem(s1, getFieldQuery(s2)), value));
        plcHandleService.writeItem(pivList);
    }

}
